package cgb2202.recruitment01.controller;

import cgb2202.recruitment01.entity.ComJob;
import cgb2202.recruitment01.entity.Emp;
import cgb2202.recruitment01.entity.JobBriefInfoTDO;
import cgb2202.recruitment01.mapper.EmpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class EmpController {
    @Autowired(required = false)
    EmpMapper mapper;

    @RequestMapping("/reg")
    public int reg(@RequestBody Emp emp) {
        Emp e = mapper.getEmpsByEmail(emp.getEmail());
        if (e != null) {
            return 2;
        }
        mapper.add(emp);
        return 1;

    }

    @RequestMapping("/selectByEmail")
    public int selectByEmail(@RequestBody Emp emp) {
        Emp e = mapper.getEmpsByEmail(emp.getEmail());
        if (e != null) {
            return 2;
        } else {
            return 1;

        }
    }

    @RequestMapping("/login")
    public int login(@RequestBody Emp emp, HttpSession session) {
        Emp e = mapper.getEmpsByEmail(emp.getEmail());
        if (e == null) {
            return 3;
        } else if (e.getPassword().equals(emp.getPassword())) {
            session.setAttribute("currentUser", e);
            return 1;
        } else return 2;
    }

    @RequestMapping("/selectAllJob")
    public List<JobBriefInfoTDO> selectAllJob() {
        return mapper.selectAllJob();
    }

    @RequestMapping("/selectJobById")
    public ComJob selectJobById(int id) {
        return mapper.selectJobById(id);
    }
}
